import { AppComponentBase } from '@shared/common/app-component-base';
import { WarehouseDTO, ServiceProxy } from './../../../../shared/service-proxy/service-proxies';
import { Component, OnInit, EventEmitter, Output, Injector } from '@angular/core';
import { FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-set-warehouse-reason-modal',
  templateUrl: './set-warehouse-reason-modal.component.html',
  styleUrls: ['./set-warehouse-reason-modal.component.less']
})
export class SetWarehouseReasonModalComponent extends AppComponentBase implements OnInit {

  @Output() modalSave = new EventEmitter<any>();
  isVisible = false;
  isOkLoading = false;
  model: WarehouseDTO;
  title: string;
  constructor(injector: Injector, private _service: ServiceProxy, private fb: FormBuilder) {
    super(injector)
  }

  ngOnInit() {
    this.model = new WarehouseDTO();
  }

  handleCancel() {
    this.isVisible = false;
  }

  handleOk() {
    this.isOkLoading = true;
    this._service.updateWarehouseStatusUsingPUT(this.model.id, this.model).subscribe(result => {
      this.isOkLoading = false;
      if (result.success) {
        this.message.success("save_success");
        this.modalSave.emit();
        this.isVisible = false;
      }
      else {
        this.message.info(this.l(result.error.message));
      }

    }, error => {
      this.isOkLoading = false;
      this.message.error(error.message);
    });
  }

  show(id: string, enableFlag: boolean) {
    this.isVisible = true;
    this.model.id = id;
    this.model.enableFlag = !enableFlag;
    if (this.model.enableFlag)
      this.title = this.l("enable");
    else
      this.title = this.l("disenable");
    this.model.reason = "";
  }
}

